package districting;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.awt.Color;

import com.vividsolutions.jts.geom.*;

public class AECPollingBoothInfo {

   public static final String CSV_PATTERN = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";

   private Nation nation;

   private FileReader reader;

   public AECPollingBoothInfo(Nation nation) {
      this.nation = nation;
   }

   public void ImportPBInfo() {

      try {
         reader = new FileReader("data/GeneralPollingPlacesDownload-15508.csv");
      } catch (FileNotFoundException e) {
         System.out.println(e.getMessage());
      }

      Scanner sc = new Scanner(reader);

      // Skip header lines
      sc.nextLine();
      sc.nextLine();

      while (sc.hasNext()) {
         String str = sc.nextLine();
         String[] field;
         field = str.split(CSV_PATTERN);

         String name = field[5];
         // System.out.println(name);
         int id = Integer.valueOf(field[3]);
         State state = nation.getState(field[0]);
         int typeID = Integer.valueOf(field[4]);
         
         //If Pre-poll centre, ignore (location isn't well determined)
         if(typeID == 5){
            continue;
         }

         Point point;
         try {
            point = new GeometryFactory().createPoint(new Coordinate(Float.valueOf(field[14]), Float
                  .valueOf(field[13])));
         } catch (ArrayIndexOutOfBoundsException e) {
            continue; // Don't bother with these polling booths.
         }
         
         if(!state.containsPoint(point)){
            System.out.println("AECPBInfo.Import: " + state.getName() + " does not contain " + name);
            continue;
         }

         Division currDivision;
         try {
            currDivision = state.getDivision(Integer.valueOf(field[1]), DivisionMethod.STATUS_QUO);
         } catch (NullPointerException e) {
            currDivision = new Division(field[2], Integer.valueOf(field[1]), DivisionMethod.STATUS_QUO);
            state.addDivision(currDivision, DivisionMethod.STATUS_QUO);
         }

         PollingBooth pb = new PollingBooth(name, id, state, point,
               currDivision);

         state.addPB(pb);
         currDivision.addPB(pb);

         /*
         System.out.println(pb.getState().getName() +" " +
          currDivision.getName() + " "+ pb.getName() + " " + id);
         System.out.println("numPB in division = " + currDivision.getBooths().size());
         */
      }

   }
   
   public void ImportPBInfo(State onlyThisState) {
      
      try {
         reader = new FileReader("data/GeneralPollingPlacesDownload-15508.csv");
      } catch (FileNotFoundException e) {
         System.out.println(e.getMessage());
      }
      
      Scanner sc = new Scanner(reader);

      // Skip header lines
      sc.nextLine();
      sc.nextLine();

      while (sc.hasNext()) {
         String str = sc.nextLine();
         String[] field;
         field = str.split(CSV_PATTERN);

         String name = field[5];
         // System.out.println(name);
         int id = Integer.valueOf(field[3]);
         State state = nation.getState(field[0]);
         if(state != onlyThisState){
            continue;
         }
         
         int typeID = Integer.valueOf(field[4]);
         
         //If Pre-poll centre, ignore (location isn't well determined)
         if(typeID == 5){
            continue;
         }

         Point point;
         try {
            point = new GeometryFactory().createPoint(new Coordinate(Float.valueOf(field[14]), Float
                  .valueOf(field[13])));
         } catch (ArrayIndexOutOfBoundsException e) {
            continue; // Don't bother with these polling booths.
         }
         
         if(!state.containsPoint(point)){
            System.out.println("AECPBInfo.Import: " + state.getName() + " does not contain " + name);
            continue;
         }

         Division currDivision;
         try {
            currDivision = state.getDivision(Integer.valueOf(field[1]), DivisionMethod.STATUS_QUO);
         } catch (NullPointerException e) {
            currDivision = new Division(field[2], Integer.valueOf(field[1]), DivisionMethod.STATUS_QUO);
            state.addDivision(currDivision, DivisionMethod.STATUS_QUO);
         }

         PollingBooth pb = new PollingBooth(name, id, state, point,
               currDivision);
       
         
         state.addPB(pb);
         currDivision.addPB(pb);

         /*
          * System.out.println(pb.getState().getName() +" " +
          * currDivision.getName() + " "+ pb.getName() + " " + id);
          * System.out.println("numPB in division = " +
          * currDivision.getBooths().size());
          */
      }

   }
}